import SAILORRequest from "./service";
import { BASE_URL, TIME_OUT } from "./config";
import eventBus from '@/utils/eventBus'
const sailorRequest = new SAILORRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptor: {
    requestInterceptor: (config) => {
      const token = localStorage.getItem("token");
      if (token) {
        config.headers!.Authorization = `Bearer ${token}`;
      }
      return config;
    },
    requestInterceptorCatch: (err) => {
      return err;
    },
    responseInterceptor: (res) => {
      return res;
    },
    responseInterceptorCatch: (err) => {
      if (err.status === 401) {
        localStorage.removeItem('token')
        eventBus.emit('app_unauthorized')
      }
      return err;
    },
  },
});
export default sailorRequest;